package com.example.tijianbackend.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.tijianbackend.dto.DetailItem;
import com.example.tijianbackend.dto.ReportDTO;
import com.example.tijianbackend.dto.ReportListDTO;
import com.example.tijianbackend.mapper.*;
import com.example.tijianbackend.pojo.*;
import com.example.tijianbackend.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: bong~
 * @description: TODO
 * @date: 2024/6/19 9:45
 * @version: 1.0
 */

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private HospitalMapper hospitalMapper;

    @Autowired
    private OverallresultMapper overallresultMapper;

    @Autowired
    private CireportMapper cireportMapper;

    @Autowired
    private CidetailedreportMapper cidetailedreportMapper;

    @Override
    public List<ReportListDTO> getReportList(String userId) {

        List<ReportListDTO> ans=new ArrayList<>();

        QueryWrapper queryWrapper1=new QueryWrapper<Orders>().eq("userId",userId).eq("state",2);
        List<Orders> ordersList=ordersMapper.selectList(queryWrapper1);

        if(ordersList==null || ordersList.size()==0){
            return ans;
        }

        List<Integer> hpIds=ordersList.stream().map(Orders::getHpId).collect(Collectors.toList());

        QueryWrapper queryWrapper2=new QueryWrapper<Hospital>().in("hpId",hpIds);
        List<Hospital> hospitalList=hospitalMapper.selectList(queryWrapper2);

        Map<Integer,Hospital> map=new HashMap<>();
        for(Hospital tmp:hospitalList){
            map.put(tmp.getHpId(),tmp);
        }

        for(Orders orders:ordersList){

            ReportListDTO dto=new ReportListDTO();
            dto.setHpId(orders.getHpId());
            dto.setHospitalName(map.get(orders.getHpId()).getName());
            dto.setOrderDate(orders.getOrderDate());
            dto.setOrderId(orders.getOrderId());
            dto.setUserId(orders.getUserId());
            dto.setSmId(orders.getSmId());

            ans.add(dto);
        }

        return ans;
    }

    @Override
    @Transactional
    public ReportDTO getReportDetailList(Integer orderId) {

        ReportDTO dto=new ReportDTO();
        QueryWrapper queryWrapper1=new QueryWrapper<Overallresult>().eq("orderId",orderId);
        dto.setOverallResultList(overallresultMapper.selectList(queryWrapper1));


        QueryWrapper queryWrapper2=new QueryWrapper<Cireport>().eq("orderId",orderId);
        List<Cireport> cireportList=cireportMapper.selectList(queryWrapper2);

        List<DetailItem> list=new ArrayList<>();
        QueryWrapper queryWrapper3;
        for(Cireport cireport:cireportList){

            Integer ciId=cireport.getCiId();


            DetailItem cur=new DetailItem(cireport);
            queryWrapper3=new QueryWrapper<Cidetailedreport>()
                    .eq("orderId",orderId).eq("ciId",ciId);

            List<Cidetailedreport> tmp=cidetailedreportMapper.selectList(queryWrapper3);

            cur.setCiDetailedList(tmp);

            list.add(cur);

        }

        dto.setDetailList(list);
        return dto;







    }
}
